package edu.ncsu.csc.itrust.dao.transaction;

import java.util.List;
import junit.framework.TestCase;
import edu.ncsu.csc.itrust.beans.TransactionBean;
import edu.ncsu.csc.itrust.dao.mysql.TransactionDAO;
import edu.ncsu.csc.itrust.datagenerators.TestDataGenerator;
import edu.ncsu.csc.itrust.enums.TransactionType;
import edu.ncsu.csc.itrust.testutils.TestDAOFactory;

public class LogTransactionTest extends TestCase {
	private TransactionDAO tranDAO = TestDAOFactory.getTestInstance().getTransactionDAO();

	private TestDataGenerator gen;
	@Override
	protected void setUp() throws Exception {
		gen = new TestDataGenerator();
		gen.clearAllTables();
		gen.transactionLog();
	}

	public void testGetAllTransactions() throws Exception {
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(8, list.size());
		// that last one inserted should be last because it was backdated
		assertEquals(1L, list.get(3).getLoggedInMID());
		assertEquals(TransactionType.DEMOGRAPHICS_EDIT, list.get(3).getTransactionType());
	}
	
	/*public void testLogSimple() throws Exception {
		tranDAO.logTransaction(TransactionType.PATIENT_REMINDERS, 9000000000L);
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(0L, list.get(0).getSecondaryMID());
		assertEquals("", list.get(0).getAddedInfo());
		assertEquals(TransactionType.PATIENT_REMINDERS, list.get(0).getTransactionType());
	}*/
	
	public void testLogFull() throws Exception {
		tranDAO.logTransaction(TransactionType.OFFICE_VISIT_EDIT, 9000000000L, 1L, "added information");
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(1L, list.get(0).getSecondaryMID());
		assertEquals("added information", list.get(0).getAddedInfo());
		assertEquals(TransactionType.OFFICE_VISIT_EDIT, list.get(0).getTransactionType());
	}
	
	/**
	 * Tests to see if the right MID number shows up in the secondaryMID column in the transactionLog.
	 * @throws Exception
	 */
	public void testSecondaryMIDHCP() throws Exception{
		tranDAO.logTransaction(TransactionType.PATIENT_CREATE, 9000000000L, 98L, "added information");
		List<TransactionBean> list = tranDAO.getAllTransactions();

		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(98L, list.get(0).getSecondaryMID());
	}
	
	public void testSecondaryMIDPatient() throws Exception{
		tranDAO.logTransaction(TransactionType.PATIENT_CREATE, 1L, 98L, "added information");
		List<TransactionBean> list = tranDAO.getAllTransactions();

		assertEquals(1L, list.get(0).getLoggedInMID());
		assertEquals(98L, list.get(0).getSecondaryMID());
	}

	public void testSecondaryMIDUAP() throws Exception{
		tranDAO.logTransaction(TransactionType.PATIENT_CREATE, 9000000001L, 98L, "added information");
		List<TransactionBean> list = tranDAO.getAllTransactions();

		assertEquals(9000000001L, list.get(0).getLoggedInMID());
		assertEquals(98L, list.get(0).getSecondaryMID());
	}
	
	/**
	 * Tests ability to add a prescription
	 * @throws Exception
	 */
	public void testAddPrescriptionLog() throws Exception {
		tranDAO.logTransaction(TransactionType.PRESCRIPTION_ADD, 9000000000L, 1L, "added a prescription");
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(1L, list.get(0).getSecondaryMID());
		assertEquals("added a prescription", list.get(0).getAddedInfo());
	}
	
	/**
	 * Tests ability to add a diagnosis
	 * @throws Exception
	 */
	public void testAddDiagnosisLog() throws Exception {
		tranDAO.logTransaction(TransactionType.ADD_DIAGNOSIS, 9000000000L, 1L, "added a diagnosis");
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(1L, list.get(0).getSecondaryMID());
		assertEquals("added a diagnosis", list.get(0).getAddedInfo());
	}
	
	/**
	 * Tests ability to add a lab procedure to the transaction log
	 * @throws Exception
	 */
	public void testAddLabProcedureLog() throws Exception {
		tranDAO.logTransaction(TransactionType.ADD_LAB_PROCEDURE, 9000000000L, 1L, "added a lab procedure");
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(1L, list.get(0).getSecondaryMID());
		assertEquals("added a lab procedure", list.get(0).getAddedInfo());
	}
	
	/**
	 * Tests ability to add a procedure to the transaction log
	 * @throws Exception
	 */
	public void testAddProcedureLog() throws Exception {
		tranDAO.logTransaction(TransactionType.ADD_PROCEDURE, 9000000000L, 1L, "added a procedure");
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(1L, list.get(0).getSecondaryMID());
		assertEquals("added a procedure", list.get(0).getAddedInfo());
	}
	
	/**
	 * Tests ability to add an immunization to the transaction log
	 * @throws Exception
	 */
	public void testAddImmunizationLog() throws Exception {
		tranDAO.logTransaction(TransactionType.ADD_IMMUNIZATION, 9000000000L, 1L, "added an immunization");
		List<TransactionBean> list = tranDAO.getAllTransactions();
		assertEquals(9, list.size());
		assertEquals(9000000000L, list.get(0).getLoggedInMID());
		assertEquals(1L, list.get(0).getSecondaryMID());
		assertEquals("added an immunization", list.get(0).getAddedInfo());
	}
}
